// .........................................................................
// Title: spreads.do
//
// Analyzes the impact of investor composition on secondary-market yield
// spread levels
// .........................................................................

* ---------------------------------------
* Construct regression panel
* ---------------------------------------

* Panel for regressions
use "$tmp/trace_merged_panel_complete_m", clear
mmerge date_m using "$tmp/gz_m", unmatched(m)
drop _merge
gsort date_m cusip
drop _*

keep if inlist(rating_cat, "AAA", "AA", "A", "BBB")
drop if missing(rating_cat_pm) | missing(drn_bucket) | missing(bc_is_floating) | missing(bc_is_senior) ///
    | missing(bc_is_callable) | missing(consolidated_issuer_id) | missing(value_outstanding)
egen _cusip = group(cusip)

gsort cusip date_m
by cusip: carryforward value_outstanding, gen(_value_outstanding)
replace value_outstanding = _value_outstanding if missing(value_outstanding)
xtset _cusip date_m

* FE categories
egen value_qtile = xtile(value_outstanding), nq(10)
egen full_fe_category = group(drn_bucket rating_cat_pm consolidated_issuer_id bc_is_senior bc_is_callable bc_is_floating value_qtile)
egen full_fe_cat_t = group(full_fe_category date_m)
save "$tmp/spreads_regression_panel", replace

* Bandwidth lag selection (Newey-West)
use "$tmp/spreads_regression_panel", clear
gsort date_m
by date_m: keep if _n == 1
count
local T = r(N)
local nw_optimal_lag = floor(0.75 * `T'^(1/3))

* ---------------------------------------
* Estimate pricing regressions
* ---------------------------------------

* Estimate specification with GZ interaction
use "$tmp/spreads_regression_panel", clear
replace bond_yield_spread = bond_yield_spread * 100

ivreghdfe bond_yield_spread share_ins_issuance c.share_ins_issuance#c.gz_spread [aw = value_outstanding], absorb(full_fe_cat_t) robust bw(`nw_optimal_lag')

local baseline_gamma = _b[share_ins_issuance]
local interaction_term = _b[c.share_ins_issuance#c.gz_spread]
local se_baseline = _se[share_ins_issuance]
local se_interaction = _se[c.share_ins_issuance#c.gz_spread]
matrix V = e(V)
local var_baseline = V[1,1]
local var_interaction = V[2,2]
local covar = V[2,1]

* Graph the total estimates as a time series (Figure 13)
use "$tmp/gz_index.dta" if date_m >= tm(2005m1), clear
gen baseline_gamma = `baseline_gamma'
gen interaction_term = `interaction_term'
gen se_baseline = `se_baseline'
gen se_interaction = `se_interaction'
gen se_total = sqrt(`var_baseline' + `var_interaction' + 2 * `covar') 
gen total_gamma = baseline_gamma + interaction_term * gz_spread
gen ci_max = total_gamma + 1.96 * se_total
gen ci_min = total_gamma - 1.96 * se_total

line total_gamma ci_max ci_min date_m, graphregion(color(white)) ///
    ytitle("Estimated Lambda (bps)") xtitle("") lcolor(red none none) lpattern(solid dash dash) legend(off) ///
    xsize(7.5) yline(0, lcolor(black)) ylab(-60(10)30, angle(0)) ///
    || rarea ci_max ci_min date_m, color(gray%20)

graph export "$graphs/yield_spreads.pdf", as(pdf) replace
